<template>
<!-- 打款审核新增页面弹框 -->
  <div>
    <div style="height:300px">
      <FormPanel
        ref="from"
        :list="initForm"
        :cols="2"
        :isSubmitBtn="false"
        :collapse="false"
        :formType="actionType"
      />
    </div>
    <div class="dialogBottmView">
      <el-button :loading="loadingFlag" @click="save()" type="primary">保 存</el-button>
      <el-button @click="closeDialog('closeDialog')">返 回</el-button>
    </div>
  </div>
</template>
<script>

import { dictionary } from '@/utils/dictMixin';
import { numberCheckMax100,positiveInteger } from '@/utils/validate';
import { addOrEditPartReturnParam } from '@/api/partsmanage/partinfos/partParameter/index';

export default {
  props:{
    actionType: {
      type: String,
      required: true,
      default: ""
    },
    row: {
      type: Object,
      required: false,
      default: null
    },
  },
  data(){
    return{
      initForm:this.createdFrom(),
      loadingFlag:false,
    }
  },
  created(){
    if(this.actionType == "edit"){
      this.init(this.row);
    }
  },
  mixins:[dictionary],
  methods:{
    init(row){
      this.initForm.forEach(x => {
        if (x.fieldName === 'returnType') {
          x.initialValue = row.returnType
        }
        if (x.fieldName === 'discountRate') {
          x.initialValue = row.discountRate
        }
        if (x.fieldName === 'describe') {
          x.initialValue = row.describe
        }
      })
    },
    async save(){//保存
        if(this.loadingFlag){
            return notifyAction('已有操作在进行中请稍后!', 'warning');
        }
        //表单校验
        let [err, data] = await this.$refs.from.GET_FORM_DATA();
        if(err) return;
        this.loadingFlag = true;
        if(this.actionType == "add"){
          data.mark = "add";
        } else {
          data.mark = "edit";
          data.id = this.row.id;
        }
        let result = await addOrEditPartReturnParam(data);
        if (result.resultCode === 200) {
          this.$notify.success({title: '提示信息',message:'保存成功!'});
          this.loadingFlag = false;
          this.closeDialog('refresh');
        } else {
          this.$notify.error({title: '提示信息',message:result.errMsg||'保存失败!'});
          this.loadingFlag = false;
        }
        
    },
    closeDialog(val){//关闭弹框
      this.$emit('close')
    },
    createdFrom(){
      return [
        {
            type: 'SELECT',
            label: '退货类型',
            fieldName: 'returnType',
            placeholder: '请选择',
            itemList: this.createDictList('3017'),
            rules: [
                {
                  required: true,
                  message: '请选择退货类型',
                  trigger: 'blur'
                }
            ]
        },
        {
            type: 'INPUT_NUMBER',
            label: '退货折扣率',
            fieldName: 'discountRate',
            rules: [
                { 
                    required: true,
                    message: '请输入退货折扣率', 
                    trigger: 'blur' 
                },
                {
                    validator: positiveInteger, 
                    trigger: 'blur' 
                },
                {
                    validator: numberCheckMax100, 
                    message: '请输入0-100的折扣率', 
                    trigger: 'blur' 
                }
            ],
        },
        {
            type: 'TEXT_AREA',
            label: '描述',
            fieldName: 'describe',
            placeholder: '请输入',
            selfCols:2,
            maxlength:150
        },
      ]
    }
  },
}
</script>
